/**
 * @author Caius
 * @description
 * @version 1.0
 * @since Created in 2024-12-30
 */

package com.itsense.stock.controller;

import com.itsense.stock.pojo.domain.PermissionPostDomain;
import com.itsense.stock.pojo.domain.PermissionsSelectAllDomain;
import com.itsense.stock.pojo.domain.SysUserDomain;
import com.itsense.stock.pojo.domain.SysUserSelectDomain;
import com.itsense.stock.pojo.entity.SysPermission;
import com.itsense.stock.pojo.entity.SysRole;
import com.itsense.stock.pojo.entity.SysUser;
import com.itsense.stock.pojo.vo.PermissionUpdateVo;
import com.itsense.stock.service.UserService;
import com.itsense.stock.vo.req.*;
import com.itsense.stock.vo.resp.LoginRespVo;
import com.itsense.stock.vo.resp.PageResult;
import com.itsense.stock.vo.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户web层Bean资源
 */
@Api(value = "/api", tags = {"用户web层Bean资源"})
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据用户名返回信息
     * @param name
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "name", value = "", required = true)
    })
    @ApiOperation(value = "根据用户名查询信息", notes = "根据用户名返回信息", httpMethod = "GET")
    @GetMapping("/user/{username}")
    public SysUser getUser(@PathVariable("username") String name){
        return userService.findByUserName(name);
    }

    /**
     * 用户登录功能
     * @param vo
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "LoginReqVo", name = "vo", value = "", required = true)
    })
    @ApiOperation(value = "用户登录功能", notes = "用户登录功能", httpMethod = "POST")
    @PostMapping("/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo vo){
        return userService.login(vo);
    }

    /**
     * 生成登录校验码的访问接口
     * @return
     */
    @ApiOperation(value = "验证码生成", notes = "生成登录校验码的访问接口", httpMethod = "GET")
    @GetMapping("/captcha")
    public R<Map> getCaptchaCode(){
        return userService.getCaptchaCode();
    }

    /**
     * 返回查询权限集合
     * @return
     */
    @ApiOperation(value = "返回查询权限集合", notes = "返回查询权限集合", httpMethod = "GET")
    @GetMapping("/permission")
    public R<List<SysPermission>> getPermission(){
        return userService.getPermission();
    }

    /**
     * 返回查看权限树
     * @return
     */
    @ApiOperation(value = "返回查看权限树", notes = "返回查看权限树", httpMethod = "GET")
    @GetMapping("/permission/tree")
    public R<List<HashMap>> getPermissionTree(){
        return userService.getPermissionTree();
    }

    /**
     * 通过post方法实现对应的用户权限的添加操作
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "PermissionPostDomain", name = "permissionPostDomain", value = "", required = true)
    })
    @ApiOperation(value = "通过post方法实现对应的用户权限的添加操作", notes = "通过post方法实现对应的用户权限的添加操作", httpMethod = "POST")
    @PostMapping("/permission")
    public R<String> addPermission(@RequestBody PermissionPostDomain permissionPostDomain){
        return userService.addPermission(permissionPostDomain);
    }

    /**
     * 更新对应的权限管理对象
     * @param vo
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "PermissionUpdateVo", name = "vo", value = "", required = true)
    })
    @ApiOperation(value = "更新对应的权限管理对象", notes = "更新对应的权限管理对象", httpMethod = "PUT")
    @PutMapping("/permission")
    public R<String> updatePermission(@RequestBody PermissionUpdateVo vo){
        return userService.updatePermission(vo);
    }

    /**
     * 通过id删除对应的权限用户
     * @param permissionId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "permissionId", value = "", required = true)
    })
    @ApiOperation(value = "通过id删除对应的权限用户", notes = "通过id删除对应的权限用户", httpMethod = "DELETE")
    @DeleteMapping("/permission/{permissionId}")
    public R<String> deletePermission(@PathVariable Long permissionId){
        return userService.deletePermission(permissionId);
    }

    /**
     * 多条件查询用户分页信息
     * @param vo
     * @return
     */
    @PostMapping("/users")
    public R<PageResult<SysUserDomain>> getUserByPage(@RequestBody GetUserReqVo vo){
        return userService.getUserByPage(vo);
    }

    @PostMapping("/user")
    public R addUser(@RequestBody UserReqVo vo){
        return userService.addUser(vo);
    }

    /**
     * 通过id查询用户信息之后返回用户具有角色信息
     * @param userId
     * @return
     */
    @GetMapping("/user/roles/{userId}")
    public R<Map<String, List>> getUserRole(@PathVariable String userId){
        return userService.getUserRole(userId);
    }

    /**
     * 更新用户角色信息
     */
    @PutMapping("/user/roles")
    public R addUserRoles(@RequestBody AddUserRolesReqVo vo){
        return userService.addUserRoles(vo);
    }

    /**
     * 批量删除用户角色信息
     * @param ids
     * @return
     */
    @DeleteMapping("/user")
    public R deleteUser(@RequestBody List<Long> ids){
        return userService.deleteUser(ids);
    }

    @GetMapping("/user/info/{userId}")
    public R<SysUserSelectDomain> findInfoByUserId(@PathVariable("userId") String userId){
        return userService.findInfoByUserId(userId);
    }

    /**
     * 通过id更新用户信息
     * @param vo
     * @return
     */
    @PutMapping("/user")
    public R updateByUserId(@RequestBody UpdateUserReqVo vo){
        return userService.updateByUserId(vo);
    }

    /**
     * 查询角色分页信息
     * @param vo
     * @return
     */
    @PostMapping("/roles")
    public R<PageResult<SysRole>> getRole(@RequestBody GetRoleReqVo vo){
        return userService.getRole(vo);
    }

    /**
     * 获取权限树结构的信息
     * @return
     */
    @GetMapping("/permission/tree/all")
    public R<List<PermissionsSelectAllDomain>> getPermissionsTreeAll(){
        return userService.getPermissionsTreeAll();
    }

    /**
     * 添加用户权限
     * @param vo
     * @return
     */
    @PostMapping("/role")
    public R addRole(@RequestBody AddRoleReqVo vo){
        return userService.addRole(vo);
    }

    /**
     * f根据角色id查询角色信息
     * @param roleId
     * @return
     */
    @GetMapping("/role/{roleId}")
    public R<List<String>> getRolePermission(@PathVariable("roleId") String roleId){
        return userService.getRolePermission(roleId);
    }

    /**
     * 更新用户权限
     * @param vo
     * @return
     */
    @PutMapping("/role")
    public R updateRolePermission(@RequestBody UpdateRolePermissionReqVo vo){
        return userService.updateRolePermission(vo);
    }

    /**
     * 删除用户权限
     * @param roleId
     * @return
     */
    @DeleteMapping("/role/{roleId}")
    public R deleteRole(@PathVariable String roleId){
        return userService.deleteRole(roleId);
    }

    /**
     * 更新用户状态
     * @param roleId
     * @param status
     * @return
     */
    @PostMapping("/role/{roleId}/{status}")
    public R updateRoleStatus(@PathVariable String roleId, @PathVariable String status){
        return userService.updateRoleStatus(roleId, status);
    }
}
